//
//  LoginSelectViewController.m
//  eshow
//
//  Created by Hisoka on 15/11/11.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "LoginSelectViewController.h"
#import "LoginViewController.h"
#import "WXApiRequestHandler.h"
#import "WXApiManager.h"

@interface LoginSelectViewController ()<WXApiManagerDelegate>

@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) UIButton *cancelBTN;
@property (nonatomic, strong) UIButton *regBTN;
@property (nonatomic, strong) UIButton *wechatBTN;

@end

@implementation LoginSelectViewController

- (void) viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setStyle];
    
    [WXApiManager sharedManager].delegate = self;
}

- (void) setStyle {
    
    self.bgView = [UIImageView newAutoLayoutView];
    [self.view addSubview:_bgView];
    
    [_bgView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.view];
    [_bgView autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [_bgView autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.view];
    [_bgView autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.view];
    
    _bgView.image = [UIImage imageNamed:@"loginBg"];
    
    self.cancelBTN = [UIButton newAutoLayoutView];
    [self.view addSubview:_cancelBTN];
    
    [_cancelBTN autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:_bgView withOffset:30];
    [_cancelBTN autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:_bgView withOffset:10];
    [_cancelBTN autoSetDimensionsToSize:CGSizeMake(60, 20)];
    _cancelBTN.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_cancelBTN setBackgroundColor:[UIColor clearColor]];
    [_cancelBTN setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelBTN addTarget:self action:@selector(cancelLogin) forControlEvents:UIControlEventTouchUpInside];
    
    self.regBTN = [UIButton newAutoLayoutView];
    [self.view addSubview:_regBTN];
    
    [_regBTN autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:_cancelBTN];
    [_regBTN autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:_bgView withOffset:-10];
    [_regBTN autoSetDimensionsToSize:CGSizeMake(80, 20)];
    [_regBTN setBackgroundColor:[UIColor blackColor]];
    _regBTN.titleLabel.textAlignment = NSTextAlignmentRight;
    [_regBTN setBackgroundColor:[UIColor clearColor]];
    [_regBTN setTitle:@"登录|注册" forState:UIControlStateNormal];
    [_regBTN addTarget:self action:@selector(pushLoginVC) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *logoImg = [UIImageView newAutoLayoutView];
    [self.view addSubview:logoImg];
    
    [logoImg autoAlignAxis:ALAxisHorizontal toSameAxisOfView:_bgView];
    [logoImg autoAlignAxis:ALAxisVertical toSameAxisOfView:_bgView];
    [logoImg autoSetDimensionsToSize:CGSizeMake(111, 75)];
    logoImg.image = [UIImage imageNamed:@"logoWhite"];
    
    self.wechatBTN = [UIButton newAutoLayoutView];
    [self.view addSubview:_wechatBTN];
    
    [_wechatBTN autoAlignAxis:ALAxisVertical toSameAxisOfView:_bgView];
    [_wechatBTN autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:_bgView withOffset:-50];
    [_wechatBTN autoSetDimensionsToSize:CGSizeMake(250, 50)];
    [_wechatBTN setBackgroundImage:[UIImage imageNamed:@"wechatLoginBTN"] forState:UIControlStateNormal];
    _wechatBTN.layer.masksToBounds = YES;
    _wechatBTN.layer.cornerRadius = 5.0f;
    [_wechatBTN addTarget:self action:@selector(wechatLogin) forControlEvents:UIControlEventTouchUpInside];

}

- (void) pushLoginVC {
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:loginVC animated:YES];
}

- (void) cancelLogin {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 微信登陆 发送请求
- (void) wechatLogin {
    [WXApiRequestHandler sendAuthRequestScope:WXAuthScope
                                        State:WXAuthState
                                       OpenID:WXAppId
                             InViewController:self];
}

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    } else {
        return YES;
    }
}

#pragma mark - 微信登录返回信息
- (void) managerDidRecvAuthResponse:(SendAuthResp *)response {
    
    if (response.code != nil) {
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        [parameters setObject:response.code forKey:@"code"];
        [parameters setObject:@"wechatLogin" forKey:@"state"];
        
        [SVProgressHUD show];
        [[HttpManager instance] requestWithMethod:@"User/wechat"
                                       parameters:parameters
                                          success:^(NSDictionary *result) {
                                              NSDictionary *userInfoTmp = [result objectForKey:@"data"];
                                              NSString *ssidTmp = [userInfoTmp objectForKey:@"user_id"];
                                              NSString *alertText = [userInfoTmp objectForKey:@"info"];
                                              
                                              if (![ssidTmp isEqualToString:@""]) {
                                                  [[TMCache sharedCache] setObject:ssidTmp forKey:@"SSID"];
                                              }
                                              
                                              [[TMCache sharedCache] setObject:userInfoTmp forKey:@"userInfo"];
                                              [[TMCache sharedCache] setObject:@"1" forKey:@"loginStatus"];
                                              
                                              [SVProgressHUD showSuccessWithStatus:alertText maskType:SVProgressHUDMaskTypeBlack];
                                              
                                              [self cancelLogin];
                                          }
                                          failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                              [SVProgressHUD showInfoWithStatus:[error localizedDescription] maskType:SVProgressHUDMaskTypeBlack];
                                          }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
